//
//  CXNetHandle.m
//  ChangZhouInHand
//
//  Created by dllo on 15/10/21.
//  Copyright © 2015年 巢鑫. All rights reserved.
//

#import "CXNetHandle.h"
#import "AFNetworking.h"

@implementation CXNetHandle

+ (void)getUrl:(NSString *)url cookie:(NSString *)cookie result:(JSONBLOCK)block
{
    //中文处理
    //NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //cookie处理
    if(cookie){
        [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    }
    
    //返回数据的格式
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil]];
    
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        //请求成功,block回调  写本地
        
        NSString *newUrl = [url componentsSeparatedByString:@"?"].lastObject;
        NSString *path_name = [NSString stringWithFormat:@"%ld.plist", [newUrl hash]];
        NSString *path_cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [path_cache stringByAppendingPathComponent:path_name];
        [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        //失败的话  读本地缓存
        
        NSString *newUrl = [url componentsSeparatedByString:@"?"].lastObject;
        NSString *path_name = [NSString stringWithFormat:@"%ld.plist", [newUrl hash]];
        NSString *path_cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [path_cache stringByAppendingPathComponent:path_name];

        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        block(result);
    }];
}

+ (void)postUrl:(NSString *)url parame:(id)parame cookie:(NSString *)cookie result:(JSONBLOCK)block
{
    
    //中文处理
//    NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //cookie处理
    if(cookie){
        [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    }
    
    //返回数据的格式
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil]];
    
    [manager POST:url parameters:parame success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSString *path_nameURLBODY = [NSString stringWithFormat:@"%@%@", url, (NSString *)parame];
        NSString *path_name = [NSString stringWithFormat:@"%ld.plist", [path_nameURLBODY hash]];
        NSString *path_cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [path_cache stringByAppendingPathComponent:path_name];
        [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
        NSString *path_nameURLBODY = [NSString stringWithFormat:@"%@%@", url, (NSString *)parame];
        NSString *path_name = [NSString stringWithFormat:@"%ld.plist", [path_nameURLBODY hash]];
        NSString *path_cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [path_cache stringByAppendingPathComponent:path_name];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        block(result);
    }];
}

@end













